#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Reflection;
using System.IO;
using System.Web.Caching;

namespace Web.Enhancements.Rest
{
    [Serializable()]
    internal class WebRestHandlerCollection : List<IHttpHandler>
    {
        internal System.Web.SessionState.SessionStateBehavior SessionMode { get; private set; }

        public WebRestHandlerCollection(System.Web.SessionState.SessionStateBehavior sessionMode)
            : base()
        {
            this.SessionMode = sessionMode;
        }

        public IHttpHandler GetHandler()
        {
            //if handler exists already - return it.
            IHttpHandler hdl = null;
            lock (this)
            {
                if (this.Count > 0)
                {
                    hdl = this[0];
                    this.RemoveAt(0);
                }
            }
            if (hdl == null)
            {
                switch (this.SessionMode)
                {
                    case System.Web.SessionState.SessionStateBehavior.Required:
                        hdl = new WebRestSessionEnabledHandler();
                        break;
                    case System.Web.SessionState.SessionStateBehavior.ReadOnly:
                        hdl = new WebRestSessionReadOnlyHandler();
                        break;
                    default:
                        hdl = new WebRestHandler();
                        break;
                }
            }
            return hdl;
        }
    }
}
